<?php

/* 文章模型(包含各种处理文章所需的方法)
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function submit_article($cate='', $tit='', $descr='', $keyw='', $cont='') {//处理提交的文章信息
    global $_DBLINK;
    $categorys = empty($cate) ? '' : $cate;
    $title = empty($tit) ? '' : $tit;
    $description = empty($descr) ? '' : $descr;
    $keywords = empty($keyw) ? '' : $keyw;
    $content = empty($cont) ? '' : $cont;

    $uid = $_SESSION['id']; //将用户id传递给文章作者id
    //将文章放到数据库中
    $sql = 'INSERT INTO `articles`(`title`,`keywords`,`u_id`,`description`,`content`,`pub_date`,`c_id`) VALUES(\'' . $title . '\',\'' . $keywords . '\',' . $uid . ',\'' . $description . '\',\'' . $content . '\',' . time() . ',\'' . $categorys . '\')';
    mysql_query($sql);
    $aid = mysql_insert_id();
    if (mysql_errno()) {
        echo 'cuowu :' . mysql_error();
    }
    if (mysql_affected_rows($_DBLINK) > 0) {
        up_rss(); //更新rss中的信息
        
        show_msg('文章发布成功！', '/admin/article.php?action=default');
    } else {
        show_msg('发布文章操作失败', '/admin/article.php?action=add');
    }
}


//添加新的文章的时候，更新rss中的内容

function up_rss() {
    //require '../include/init.php';
    session_start();
    //将最新文章的标题 url  简介从数据库中取出来，放在rss中
    $sql = 'SELECT `title`,`description`,`id` FROM `articles` ORDER BY `id` DESC LIMIT 5';
    $result = mysql_query($sql);
    $data = array();
    while ($rows = mysql_fetch_assoc($result)) {
        $data[] = $rows;
    }
    $now = date("D, d M Y H:i:s T");
    $xmls = <<<hello
<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0'>
    <channel>
        <title>phplinblog海阔凭鱼跃，天高任鸟飞</title>
        <link>http://lin.97md.net</link>
        <description>凡人皆逐境生心。心随欣厌。若欲无境当忘其心。心忘则境空。境空则心灭。不忘心而除境。境不可除。只益纷扰耳。故万法唯心心亦不可得。</description>
<language>en-us</language>
　　　　　　　　　　<pubDate>$now</pubDate>
　　　　　　　　　　<lastBuildDate>$now</lastBuildDate>
hello;


    foreach ($data as $row) {
        $xmls.="<item><title>" . $row['title'] . "</title>";
        $url = 'http://lin.97md.net/index.php?aid=' . $row['id'];
        $xmls.="<link>" . $url . "</link>";
        $xmls.="<description>" . $row['description'] . "</description></item>";
    }
    $xmls.="</channel></rss>";
//将xmls写入rss.xml文件中
    $file = fopen('../rss/rss.xml', w);
    fwrite($file, $xmls);
    fclose($file);
}

//发布文章页面的“文章分类”的选择
function selcate($cid='') {
    $sql = 'SELECT `id`,`title` FROM `categorys`';
    $result = mysql_query($sql);

    $return = '<option >--选择--</option>';
    while ($cate = mysql_fetch_assoc($result)) {
        $id = $cate['id'];
        $cates = $cate['title'];
        if ($id == $cid) {
            $return.='<option value=\'' . $id . '\' selected>' . $cates . '</option>';
        } else {
            $return.='<option value=\'' . $id . '\'>' . $cates . '</option>';
        }


        // foreach ($cates as $value) {
        // $return.='<option name=\''.$id.'\'>' . $cates. '</option>';
        //}
    }

    return $return;
}

//‘管理文章’页面，输出检出的文章
function manart() {
    $sql = 'SELECT `title`,`u_id`,`pub_date`,`id`,`c_id` FROM `articles` ORDER BY `pub_date` DESC';
    $result = mysql_query($sql);

    while ($rows = mysql_fetch_row($result)) {
        $rows[2] = date('Y-m-d H:i:s', $rows[2]); //将时间戳换成普通的时间格式
        $uid = $rows[1];
        //根据文章中的作者编号，找到user用户的名字
        $sql1 = 'SELECT `login_name` FROM `users` WHERE `id`=' . $uid . '';
        $result1 = mysql_query($sql1);
        $unames = mysql_fetch_row($result1);
        $uname = $unames[0];
        $rows[1] = $uname;
        $cid = $rows[4]; //取出分类的id
        $aid = $rows[3]; //取出文章的id号
        for ($i = 0; $i < 3; $i++) {
            $row[$i] = $rows[$i];
        }

        $return.= '<div class="rows">';
        foreach ($row as $value) {
            $return.='<div class="cols">';
            $return.=$value;
            $return.='</div>';
        }
        $return.='<div class="cols">';
        $return.=manner($aid, $cid);
        $return.='</div>';
        $return.='</div>';
        $return.='<div class="ro"></div>';
    }


    return $return;
}

function manner($aid, $cid) {
    $manner = "<a href=''>[锁定]</a>&nbsp;";
    $manner.="<a href='article.php?action=edit&aid=" . $aid . "&cid=" . $cid . "'>[修改]</a>&nbsp;";
    $manner.="<a href='article.php?action=del&aid=" . $aid . "'>[删除]</a>";

    return $manner;
}

//function:编辑器
function get_editor($content='') {
    include WEB_ROOT . '/admin/fckeditor/index.php';
    $fck = new FCKeditor('FCKeditor1');
    $fck->BasePath = './fckeditor/'; //fckeditor所在的位置
    $fck->Width = '100%';
    $fck->Height = "300";
    $fck->ToolbarSet = "Default";
    $fck->Value = $content;
    $fck_editor = $fck->CreateHtml();

    return $fck_editor;
}

//处理修改文章时提交的信息
function edit_article($cate='', $tit='', $descr='', $keyw='', $cont='', $aid='') {
    global $_DBLINK;
    $categorys = empty($cate) ? '' : $cate;
    $title = empty($tit) ? '' : $tit;
    $description = empty($descr) ? '' : $descr;
    $keywords = empty($keyw) ? '' : $keyw;
    $content = empty($cont) ? '' : $cont;

    $uid = $_SESSION['id']; //将用户id传递给文章作者id
    //将文章相关信息更新
    $sql = 'UPDATE `articles` SET `title`=\'' . $title . '\',`keywords`=\'' . $keywords . '\',`u_id`=\'' . $uid . '\',`description`=\'' . $description . '\',`content`=\'' . $content . '\',`pub_date`=\'' . time() . '\',`c_id`=\'' . $categorys . '\' WHERE `id`=\'' . $aid . '\'';
    // $sql = 'INSERT INTO `articles`(`title`,`keywords`,`u_id`,`description`,`content`,`pub_date`,`c_id`) VALUES(\'' . $title . '\',\'' . $keywords . '\',' . $uid . ',\'' . $description . '\',\'' . $content . '\',' . time() . ',\'' . $categorys . '\')';
    mysql_query($sql);

    show_err(); //显示sql语句的错误信息
    if (mysql_affected_rows($_DBLINK) > 0) {
        up_rss(); //更新rss中的信息
        show_msg('文章修改成功！', '/admin/article.php?action=default');
    } else {
        show_msg('修改文章操作失败', '/admin/article.php?action=default');
    }
}

//删除文章的方法
function del_art($aid) {
    global $_DBLINK;
    $sql = 'DELETE FROM `articles` WHERE `id`=' . $aid . '';
    mysql_query($sql);

    if (mysql_errno()) {
        echo 'cuowu :' . mysql_error();
    }
    if (mysql_affected_rows($_DBLINK) > 0) {
        up_rss(); //更新rss中的信息
        show_msg('文章删除成功！', '/admin/article.php?action=default');
    } else {
        show_msg('文章删除失败！', '/admin/article.php?action=default');
    }
}

//修改文章时，用下面的方法，给value附上值
function show_art($aid) {
    $articles = array();
    $sql = 'SELECT `c_id`,`title`,`keywords`,`description`,`content` FROM `articles` WHERE `id`=' . $aid . '';
    $result = mysql_query($sql);

    while ($rows = mysql_fetch_assoc($result)) {
        foreach ($rows as $key => $values) {
            $articles[$key] = $values;
        }
    }
    //返回一个包含‘文章信息’的数组
    return $articles;
}
